<template>
  <div class="login-container" @keyup.enter="handleLogin" id="loginId" ref="login">
    <!-- <div class="login-time">
      {{ time }}
    </div> -->
    <div class="login-weaper">
      <div class="login-border animate__animated animate__fadeInLeft">
        <div class="login-main">
          <p class="login-title">
            <!-- {{ $t('login.title') }}{{ website.title }}
            <top-lang></top-lang> -->
            {{ infoVal.sysName || '血液系统疾病临床医学研究大数据平台' }}
          </p>
          <userLogin
            v-if="activeName === 'user'"
            @clickActiveName="clickActiveName"
            :infoVal="infoVal"
          ></userLogin>
          <codeLogin v-else-if="activeName === 'code'"></codeLogin>
          <thirdLogin v-else-if="activeName === 'third'"></thirdLogin>
          <registerLogin
            v-else-if="activeName === 'register'"
            @clickActiveName="clickActiveName"
          ></registerLogin>
          <!-- <div class="login-menu">
            <el-link href="#" @click.stop="activeName = 'user'">{{
              $t('login.userLogin')
            }}</el-link>
            <el-link href="#" @click.stop="activeName = 'code'">{{
              $t('login.phoneLogin')
            }}</el-link>
            <el-link href="#" @click.stop="activeName = 'third'">{{
              $t('login.thirdLogin')
            }}</el-link>
            <el-link
              :href="
                website.oauth2.ssoBaseUrl + website.oauth2.ssoAuthUrl + website.oauth2.redirectUri
              "
              >{{ $t('login.ssoLogin') }}</el-link
            >
          </div> -->
        </div>
      </div>
      <div class="login-left animate__animated animate__fadeInRight">
        <div class="top-logo" style="justify-content: center;height: 260px;">
          <!-- :style="{
              height: infoVal.backgroundUrl ? '70%' : 'auto',
              width: infoVal.backgroundUrl ? '17%' : '150px',
              padding: infoVal.backgroundUrl ? '5%' : '0',
            }" -->
          <img
            style="width: 120px;max-height: 180px;"
            :src="infoVal.backgroundUrl ? infoVal.backgroundUrl : '/img/logo1.jpeg'"
            alt=""
          />
          <img style="width: 120px;max-height: 180px;margin: 0 80px;" src="/img/logo2.jpeg" alt="" />
          <img style="width: 120px;max-height: 180px;" src="/img/ZKRY1.png" alt="" />
        </div>
        <div style="width: 100%;height: auto;display:flex;align-items: center;justify-content: center;">
          <img style="width: 600px;" class="img" src="/img/login.png" alt="" />
        </div>
        <!-- <p class="title">{{ $t('login.info') }}</p> -->
        <p class="title">登录视为您已同意账号绑定协议、用户协议、隐私政策、产品服务协议</p>
      </div>
    </div>
  </div>
</template>
<script>
import userLogin from './userlogin.vue';
import registerLogin from './registerlogin.vue';
import codeLogin from './codelogin.vue';
import thirdLogin from './thirdlogin.vue';
import { mapGetters } from 'vuex';
import { validatenull } from '@/utils/validate';
import topLang from '@/page/index/top/top-lang.vue';
import { getQueryString, getTopUrl } from '@/utils/util';
import website from '@/config/website';
import { info } from '@/api/system/tenant';

export default {
  name: 'login',
  components: {
    userLogin,
    registerLogin,
    codeLogin,
    thirdLogin,
    topLang,
  },
  data() {
    return {
      website: website,
      time: '',
      activeName: 'user',
      socialForm: {
        tenantId: '000000',
        source: '',
        code: '',
        state: '',
      },
      infoVal: {},
    };
  },
  watch: {
    $route() {
      this.handleLogin();
    },
  },
  created() {
    this.handleLogin();
    this.getTime();
  },
  mounted() {
    this.getTenant();
  },
  beforeDestroy() {},
  computed: {
    ...mapGetters(['tagWel']),
  },
  props: [],
  methods: {
    getTenant() {
      let domain = window.location.origin; //格式为http://192.168.1.119:2888
      info(domain).then(res => {
        const data = res.data;
        if (data.success && data.data.tenantId) {
          this.infoVal = data.data;
        }
      });
    },
    clickActiveName(activeName) {
      this.activeName = activeName;
    },
    getTime() {
      setInterval(() => {
        this.time = this.$dayjs().format('YYYY年MM月DD日 HH:mm:ss');
      }, 1000);
    },
    handleLogin() {
      const topUrl = getTopUrl();
      const redirectUrl = '/oauth/redirect/';
      const ssoCode = '?code=';
      this.socialForm.source = getQueryString('source');
      this.socialForm.code = getQueryString('code');
      this.socialForm.state = getQueryString('state');
      if (validatenull(this.socialForm.source) && topUrl.includes(redirectUrl)) {
        let source = topUrl.split('?')[0];
        source = source.split(redirectUrl)[1];
        this.socialForm.source = source;
      }
      if (
        topUrl.includes(redirectUrl) &&
        !validatenull(this.socialForm.source) &&
        !validatenull(this.socialForm.code) &&
        !validatenull(this.socialForm.state)
      ) {
        const loading = this.$loading({
          lock: true,
          text: '第三方系统登录中,请稍后',
          background: 'rgba(0, 0, 0, 0.7)',
        });
        this.$store
          .dispatch('LoginBySocial', this.socialForm)
          .then(() => {
            window.location.href = topUrl.split(redirectUrl)[0];
            //加载工作流路由集
            this.loadFlowRoutes();
            this.$router.push(this.tagWel);
            loading.close();
          })
          .catch(() => {
            loading.close();
          });
      } else if (
        !topUrl.includes(redirectUrl) &&
        !validatenull(this.socialForm.code) &&
        !validatenull(this.socialForm.state)
      ) {
        const loading = this.$loading({
          lock: true,
          text: '单点系统登录中,请稍后',
          background: 'rgba(0, 0, 0, 0.7)',
        });
        this.$store
          .dispatch('LoginBySso', this.socialForm)
          .then(() => {
            window.location.href = topUrl.split(ssoCode)[0];
            //加载工作流路由集
            this.loadFlowRoutes();
            this.$router.push(this.tagWel);
            loading.close();
          })
          .catch(() => {
            loading.close();
          });
      }
    },
    loadFlowRoutes() {
      this.$store.dispatch('FlowRoutes').then(() => {});
    },
  },
};
</script>
<script setup>
import autofit from 'autofit.js';
import {
  ref,
  onBeforeMount,
  onMounted,
  onBeforeUpdate,
  onUpdated,
  onBeforeUnmount,
  onUnmounted,
} from 'vue';
onMounted(() => {
  autofit.init(
    {
      dh: 1080,
      dw: 1920,
      el: '#loginId',
      resize: true,
    },
    false
  );
});
onUnmounted(() => {
  autofit.off();
});
</script>